<!DOCTYPE html>

<html>
<head>
  <title>sockets.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div class="container">
    <div class="page">

      <div class="header">
        
          <h1>sockets.coffee</h1>
        

        
          <div class="toc">
            <h3>Table of Contents</h3>
            <ol>
              
                
                <li>
                  <a class="source" href="admin.html">
                    admin.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.auditData.html">
                    api.auditData.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.auditEvent.html">
                    api.auditEvent.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.html">
                    api.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.entity.html">
                    api.entity.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.map.html">
                    api.map.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.server.html">
                    api.server.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.variable.html">
                    api.variable.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="api.view.html">
                    api.view.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="app.instance.html">
                    app.instance.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="appjs.html">
                    appjs.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="data.html">
                    data.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="dataUtil.html">
                    dataUtil.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="manager.html">
                    manager.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="messages.html">
                    messages.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditData.html">
                    auditData.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditEvent.html">
                    auditEvent.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="base.html">
                    base.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entityDefinition.html">
                    entityDefinition.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entityObject.html">
                    entityObject.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="eventAction.html">
                    eventAction.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="eventRule.html">
                    eventRule.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="link.html">
                    link.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="map.html">
                    map.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shape.html">
                    shape.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="user.html">
                    user.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="userSettings.html">
                    userSettings.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="variable.html">
                    variable.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="adminRoutes.html">
                    adminRoutes.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="appRoutes.html">
                    appRoutes.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="settings.html">
                    settings.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="sockets.html">
                    sockets.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="tutorial.html">
                    tutorial.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="vectors.html">
                    vectors.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="statusTabView.html">
                    statusTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="toolsTabView.html">
                    toolsTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="usersTabView.html">
                    usersTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="adminView.html">
                    adminView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="alertView.html">
                    alertView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditDataManagerView.html">
                    auditDataManagerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="auditEventManagerView.html">
                    auditEventManagerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="baseView.html">
                    baseView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="createMapView.html">
                    createMapView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entityManagerView.html">
                    entityManagerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="footerView.html">
                    footerView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="helpView.html">
                    helpView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="entitiesTabView.html">
                    entitiesTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="inspectorTabView.html">
                    inspectorTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="mapTabView.html">
                    mapTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapeTabView.html">
                    shapeTabView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="controlsView.html">
                    controlsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="labelEditView.html">
                    labelEditView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="linkCreatorView.html">
                    linkCreatorView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="linkLabelsView.html">
                    linkLabelsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="linkView.html">
                    linkView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapeLabelsView.html">
                    shapeLabelsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapeView.html">
                    shapeView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="shapesMoverView.html">
                    shapesMoverView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="mapView.html">
                    mapView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="menuView.html">
                    menuView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="overlayView.html">
                    overlayView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="scriptEditorView.html">
                    scriptEditorView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="settingsView.html">
                    settingsView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="startView.html">
                    startView.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="variableManagerView.html">
                    variableManagerView.coffee
                  </a>
                </li>
              
            </ol>
          </div>
        
      </div>

      
        
        <h2 id="sockets">SOCKETS</h2>

        
      
        
        <p>Handle Socket.IO communication with the Node.js server. Using sockets to
push messages from/to the server is much more efficient than pooling it
for new data.</p>

        
          <div class='highlight'><pre>SystemApp.Sockets =

    <span class="attribute">socket</span>: <span class="literal">null</span>      <span class="comment"># the Socket.IO object</span>
    <span class="attribute">serverErrors</span>: []  <span class="comment"># holds a cached list of errors which happened on the server</span></pre></div>
        
      
        
        <h2 id="starting-and-stopping">STARTING AND STOPPING</h2>

        
      
        
        <p>Start listening to Socket.IO messages from the server.</p>

        
          <div class='highlight'><pre>    <span class="attribute">start</span>:<span class="function"> -&gt;</span>
        <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@socket</span>?
            url = window.location
            <span class="property">@socket</span> = io.connect <span class="string">"http://<span class="subst">#{url.hostname}</span>:<span class="subst">#{url.port}</span>"</span>

        <span class="property">@socket</span>.<span class="literal">on</span> <span class="string">"connection-count"</span>, <span class="property">@onServerConnectionCounter</span>
        <span class="property">@socket</span>.<span class="literal">on</span> <span class="string">"server:error"</span>, <span class="property">@onServerError</span>
        <span class="property">@socket</span>.<span class="literal">on</span> <span class="string">"entitydata:refresh"</span>, <span class="property">@onEntityDataRefresh</span>
        <span class="property">@socket</span>.<span class="literal">on</span> <span class="string">"auditdata:refresh"</span>, <span class="property">@onAuditDataRefresh</span>
        <span class="property">@socket</span>.<span class="literal">on</span> <span class="string">"clients:refresh"</span>, <span class="property">@onClientsRefresh</span></pre></div>
        
      
        
        <p>Stop listening to all socket messages from the server. Please note that this
will NOT kill the socket connection.</p>

        
          <div class='highlight'><pre>    <span class="attribute">stop</span>:<span class="function"> -&gt;</span>
        <span class="property">@socket</span>.<span class="literal">off</span>()</pre></div>
        
      
        
        <h2 id="socket-listen">SOCKET LISTEN</h2>

        
      
        
        <p>Listen to connection counter updates. This happens everytime someone a new browser
window pointing to the System app is opened or closed.</p>

        
          <div class='highlight'><pre>    <span class="attribute">onServerConnectionCounter</span>: <span class="function"><span class="params">(count)</span> -&gt;</span>
        SystemApp.footerView?.setOnlineUsers count</pre></div>
        
      
        
        <p>listen to server errors. A <a href="alertView.html">footer alert</a> will be shown
and errors can also be listed on the <a href="menuView.html">Settings menu</a>.</p>

        
          <div class='highlight'><pre>    <span class="attribute">onServerError</span>: <span class="function"><span class="params">(err)</span> -&gt;</span>
        err.timestamp = <span class="keyword">new</span> Date()
        SystemApp.Sockets.serverErrors.push err
        SystemApp.serverEvents.trigger <span class="string">"error"</span>, err</pre></div>
        
      
        
        <p>Listen to <a href="entityObject.html">Entity Definition data</a> refreshes.</p>

        
          <div class='highlight'><pre>    <span class="attribute">onEntityDataRefresh</span>: <span class="function"><span class="params">(entityDef)</span> -&gt;</span>
        updated = SystemApp.Data.entities.getByFriendlyId entityDef.friendlyId</pre></div>
        
      
        
        <p>Only proceed if entity was found on the <a href="data.html">data store</a>.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> <span class="keyword">not</span> updated?
            SystemApp.consoleLog <span class="string">"Sockets entitydata:refresh"</span>, <span class="string">"Could not find entity <span class="subst">#{entityDef.friendlyId}</span>."</span>, entityDef
            <span class="keyword">return</span>

        updated.data entityDef.data
        updated.lastDataRefresh = <span class="keyword">new</span> Date()</pre></div>
        
      
        
        <p>Listen to <a href="auditData.html">AuditData</a> refreshes.</p>

        
          <div class='highlight'><pre>    <span class="attribute">onAuditDataRefresh</span>: <span class="function"><span class="params">(auditData)</span> -&gt;</span>
        updated = SystemApp.Data.auditData.getByFriendlyId auditData.friendlyId</pre></div>
        
      
        
        <p>Only proceed if entity was found on the <a href="data.html">data store</a>.</p>

        
          <div class='highlight'><pre>        <span class="keyword">if</span> <span class="keyword">not</span> updated?
            SystemApp.consoleLog <span class="string">"Sockets auditdata:refresh"</span>, <span class="string">"Could not find audit data <span class="subst">#{auditData.friendlyId}</span>."</span>, auditData
            <span class="keyword">return</span>

        updated.data auditData.data
        updated.lastDataRefresh = <span class="keyword">new</span> Date()</pre></div>
        
      
        
        <p>Trigger the audit data refresh to the app.</p>

        
          <div class='highlight'><pre>        SystemApp.dataEvents.trigger <span class="string">"auditdata:refresh"</span>, updated</pre></div>
        
      
        
        <p>Force reload the app by refreshing the browser window.</p>

        
          <div class='highlight'><pre>    <span class="attribute">onClientsRefresh</span>: <span class="function"><span class="params">(data)</span> -&gt;</span>
        ms = data.seconds * <span class="number">1000</span>
        SystemApp.routes?.refresh ms</pre></div>
        
      
        
        <h2 id="socket-send">SOCKET SEND</h2>

        
      
        
        <p>Trigger the &quot;clients:refresh&quot; command on all connected browsers. This will force
clients to refresh the window in X seconds.</p>

        
          <div class='highlight'><pre>    <span class="attribute">sendClientsRefresh</span>: <span class="function"><span class="params">(seconds)</span> -&gt;</span>
        <span class="keyword">if</span> <span class="keyword">not</span> SystemApp.Data.loggedUser.hasRole(<span class="string">"admin"</span>)
            SystemApp.consoleLog <span class="string">"Sockets.sendClientRefresh"</span>, <span class="string">"You don't have the necessary permissions!"</span>
            <span class="keyword">return</span></pre></div>
        
      
        
        <p>Default seconds is 10.</p>

        
          <div class='highlight'><pre>        seconds = SystemApp.Settings.sockets.clientRefreshSeconds <span class="keyword">if</span> <span class="keyword">not</span> seconds?</pre></div>
        
      
        
        <p>Emit event.</p>

        
          <div class='highlight'><pre>        SystemApp.Sockets.socket.emit <span class="string">"clients:refresh"</span>, {<span class="attribute">seconds</span>: seconds}</pre></div>
        
      
      <div class="fleur">h</div>
    </div>
  </div>
</body>
</html>
